package com.camartsApp.controller;

import com.camartsApp.Util.PhotoMetadataUtil;
import com.camartsApp.common.ResultVO;
import com.camartsApp.Util.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("/api/photo")

public class PhotoPreviewController {

    @Autowired
    private PhotoMetadataUtil photoMetadataUtil;

    /**
     * 接收图片文件，读取元数据返回前端，供用户预览
     */
    @PostMapping("/preview")
    public ResultVO previewMetadata(@RequestParam("file") MultipartFile file) {
        try {
            // 调用工具类读取元数据
            Map<String, Object> metadata = photoMetadataUtil.extractMetadata(file);
            if (metadata == null || metadata.isEmpty()) {
                return Response.FailResultByMsg("未能读取图片元数据");
            }
            return Response.OkByData(metadata);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.FailResultByMsg("读取照片元数据失败：" + e.getMessage());
        }
    }
}